# 变量的传递机制
x = [1, 2, 3]
y = x
x[1] = 5
print(y)  # [1, 5, 3]
"""
显然我们没有改动 y 但是 y和预期不一样
是因为 x 在内存中指向 [1, 2, 3]
y = x 是将引用传递给 y 共用‘一个’[1, 2, 3]
"""

# 浅拷贝
x = [1, 2, 3]
y = x[:]
# y = x.copy() 效果一样
x[1] = 5
print(y)  # [1, 2, 3]

x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = x[:]
x[1][1] = 0
print(y)
# [[1, 2, 3], [4, 0, 6], [7, 8, 9]] 发现内容还是和预期的不一样 这就需要深拷贝


# 深拷贝
# 导包
import copy

# copy包的浅拷贝
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = copy.copy(x)
x[1][1] = 0
print(y)
# [[1, 2, 3], [4, 0, 6], [7, 8, 9]]

x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = copy.deepcopy(x)
x[1][1] = 0
print(y)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# 为了效率 默认是使用浅拷贝
